<template>
	<div class="sample-info">
		<div style="padding:0 20px;height:100%;">
			<div style="padding:10px 0;">
				监测点列表
				<div style="float:right;margin-top: -4px;">
					<el-button :type="editDisable?'primary':'success'" size="mini" @click="addNextPoint()">添加QC</el-button>
					<el-button type="danger" size="mini" @click="deleteItem" :disabled="editDisable">删除</el-button>


				</div>
			</div>
			<el-table :data="sampleInfoData" style="width: 100%" size="mini" height="calc(100% - 50px)" cell-class-name="monitorPointTableCell"
			 highlight-current-row @current-change="currentChange" @selection-change="handleSelectionChange" class='tableCon' ref="sampleTable">
				<el-table-column type="selection" min-width="38px" :key="Math.random()" fixed>
				</el-table-column>
				<el-table-column min-width="80px" label="序号" fixed>
					<template slot-scope="scope">
						{{scope.$index+1}}
					</template>
				</el-table-column>
				<el-table-column property="name" label="名称" min-width="120px" show-overflow-tooltip>
				</el-table-column>

				<el-table-column min-width="150px" property="monitorZoneName" label="监测区域" show-overflow-tooltip>

				</el-table-column>

				<el-table-column property="monitor" min-width="100px" :formatter="monitorFormatter" label="监测点" show-overflow-tooltip>
				</el-table-column>
				<el-table-column property="monitorType" min-width="100px" label="监测点类型" :formatter="monitorTypeFormatter">
				</el-table-column>

				<el-table-column property="qcType" min-width="100px" label="QC类型" :formatter="qcTypeFormatter">
				</el-table-column>


			</el-table>

			<addPoint ref="addPointChild" :selectedArr="monitorPoints" :showAddpoint.sync="isShowAddpoint" :sampleData.sync="sampleInfoData"
			 :sampleForm.sync="sampleForm" :currentArr="currentArr"></addPoint>
		</div>
		<el-row style="text-align: center;" class="commonFooter">
			<el-button @click="changeStep('selectCheckPiont')" size="mini">上一步</el-button>
			<el-button type="primary" @click="changeStep('addCheckItem')" size="mini">下一步</el-button>
		</el-row>
	</div>
</template>

<script>
	import { mapState } from 'vuex'
	import addBottle from './sampleDialog/addBottle'
	import addPoint from './sampleDialog/addPoint'
	export default {
		components: {
			addBottle,
			addPoint
		},
		name: 'add-qc',
		props: ['newTaskBasic', 'createType', 'newTaskPointList'],
		data() {
			return {
				sampleInfoData: [],
				addIndex: '',
				sampleForm: {},
				addPointTitle: '',
				editDisable: true,
				isShowAddpoint: false,
				isshowAddBottle: false,
				monitorPoints: [],
				allMonitorZone: [],//获取所有检测区域信息
				allMonitor: [],//所有监测点的信息
				currentArr: {},//当前选中行
			}
		},
		computed: {
			...mapState(['status', 'isBD', 'typeQcSelect', 'EmployeeByLabs', 'monitorSelect', 'tableHeightC']),
		},
		mounted() {
			this.sampleInfoData = this.newTaskPointList
			this.$store.dispatch('getEmployeeByLab')
			this.$store.dispatch('getAllSubcontractor')
			this.getAllMonitorZone();
			this.getallMonitor()

		},
		methods: {
			selectInit(row, index) { //判断样品列表是否可勾选 是qc样品不可勾选
				if (row.qcType == null || row.qcType == '' || row.qcType == undefined) {
					return true  //可勾选
				} else {
					return false  //不可勾选
				}
			},
			currentChange(val) {
				this.currentArr = val
				if (this.currentArr == undefined) {
					this.currentArr = {};
				}
			},
			// 删除监测点
			deleteItem() { 
				var deleMonitors = [];//选出所有需要删除的监测点（样品）
				for (let j = 0; j < this.monitorPoints.length; j++) { //遍历每一个勾选的监测点
				
					if (this.monitorPoints[j].qcType == null || this.monitorPoints[j].qcType == undefined || this.monitorPoints[j].qcType == "") { //如果质控类型为空，则为父样，则需找到子样删除
						for (let i = 0; i < this.sampleInfoData.length; i++) {
							if (this.sampleInfoData[i].parent == this.monitorPoints[j].parent) { //找到子样
								deleMonitors.push(this.sampleInfoData[i]);
							}
						}
					}else{
						deleMonitors.push(this.monitorPoints[j]); //不管勾选的是不是父样都要把自己本身加入
					}
				}
				deleMonitors.forEach(item => {
					this.sampleInfoData.splice(this.sampleInfoData.indexOf(item), 1)
				})
			},
			changeStep(tabName) {
				if(tabName == 'selectCheckPiont'){  //回到上一步的话删除添加的质控样
					var deleMonitors = [];
					deleMonitors = this.sampleInfoData.filter(item => item.qcType != undefined);
					deleMonitors.forEach(item => {
						this.sampleInfoData.splice(this.sampleInfoData.indexOf(item), 1)
					})
					
				}
				this.$parent.$parent.changeTab(tabName)
				
				this.$parent.$parent.showNewTaskData()
			},
			monitorZoneFormatter(row, column, cellValue, index) {
				let filterArr = [];
				filterArr = this.allMonitorZone.filter(item => item.id === cellValue)
				if (filterArr.length != 0) {
					return filterArr[0].name
				}

			},
			monitorFormatter(row, column, cellValue, index) {
				let filterArr = [];
				filterArr = this.allMonitor.filter(item => item.id === cellValue)
				if (filterArr.length != 0) {
					return filterArr[0].name
				}
			},
			monitorTypeFormatter(row, column, cellValue, index) {

				let filterArr = [];
				filterArr = this.monitorSelect.itemList.filter(item => item.code === cellValue)
				if (filterArr.length != 0) {
					return filterArr[0].name
				}


			},
			//			格式化人员
			collectionFormatter(row, column, cellValue, index) {
				let filterArr = [];
				filterArr = this.EmployeeByLabs.filter(item => item.account === cellValue)
				if (filterArr.length != 0) {
					return filterArr[0].name
				}

			},
			//			格式化qc值
			qcTypeFormatter(row, column, cellValue, index) {
				let filterArr = [];
				filterArr = this.typeQcSelect.itemList.filter(item => item.code === cellValue)
				if (filterArr.length != 0) {
					return filterArr[0].name
				}

			},

			//			获取所有检测区域
			getAllMonitorZone() {
				this.$http({
					method: 'post',
					apiUrl: '/client/readMonitorZones.htm',
					params: { fRef: this.newTaskBasic.client },
					isJson: true,
				}).then(res => {
					if (res.code === 0) {

						this.allMonitorZone = res.data
					} else {
						//				          	reject(err)
					}
				}, )

			},
			//			获取所有监测点
			getallMonitor() {
				this.$http({
					method: 'post',
					apiUrl: '/client/readMonitors.htm',
					params: { fRef: '', id: this.newTaskBasic.client },
					isJson: true,
				}).then(res => {
					if (res.code === 0) {

						this.allMonitor = res.data
					} else {
						//				          	reject(err)
					}
				}, )
			},

			addNextPoint() {
				this.sampleForm = {
					analyst: "",
					analystName: "",
					id: "",
					monitor: "",
					monitorName: "",
					monitorType: "",
					monitorZone: "",
					monitorZoneName: "",
					name: "",
					parent: "",
					itemBeans: [],
					sampleDetailBeans: [],
					subcontractor: "",
					subcontractorName: ""
				}
				this.addPointTitle = '添加质控'
				this.isShowAddpoint = true;

			},
			handleSelectionChange(val) {
				this.monitorPoints = val;
			},

			editBottle() {
				this.isshowAddBottle = true
			},
			confirm() {
				this.$parent.$parent.close()
				this.$parent.$parent.$parent.showButton = {
					saveButton: true,
					submitButton: true,
					auditButton: false
				}
				this.$parent.$parent.$parent.callTaskDetail(this.createType, [0, 1])
			}
		},
		watch: {
			monitorPoints: {
				handler: function (newValue, oldValue) {

					if (this.monitorPoints.length != 0) {
						this.editDisable = false
					} else {
						this.editDisable = true
					}
				},
				deep: true
			},
			newTaskPointList: {
				handler: function (newValue, oldValue) {
					this.sampleInfoData = this.newTaskPointList
				},
				deep: true
			}

		}
	}

</script>

<style>

</style>